<template>
  <div class="article-list">
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell v-for="(article, index) in list" :key="index" :title="article.title" />
    </van-list>
  </div>
</template>

<script>
import { getUserArticle } from '@/api/article';

export default {
  name: 'ArticleList',
  props: {
    channel: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: null,
    };
  },
  created() {},
  computed: {},
  methods: {
    async onLoad() {
      try {
        const { data } = await getUserArticle({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(),
          with_top: 1,
        });
        const { results } = data.data;
        // console.log(results);

        // ... 数组的展开操作符，他会把数组元素一个个拿出来
        this.list.push(...results);

        if (results.length) {
          this.timestamp = data.data.pre_timestamp;
          console.log(data);
        } else {
          this.finished = true;
        }
        this.loading = false;
      } catch (err) {
        console.log('请求失败', err);
      }
    },
  },
};
</script>

<style lang="less" scoped></style>
